<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
//use App\Http\Requests;
use App\Http\Requests\NewsRequest;
use Request;

use App\Http\Controllers\Controller;
use App\News;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;
class NewsController extends Controller
{
    public function  /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */ __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $news=News::all();
        return view('news.news',compact('news'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
        return view('news.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(NewsRequest $request)
    {
        $file = Request::file('fileName');
        if($file==null)
        {
            session()->flash('uploadImage','您没有上传图片，请上传图片');
            return redirect('/news/create');
        }
        else
        {
            Storage::disk('local')->put($file->getClientOriginalName(),  File::get($file));
            $title=$request->input('title');
            $abstract=$request->input('abstract');
            $body=$request->input('body');
            $news=New News();
            $news->title=$title;
            $news->abstract=$abstract;
            $news->imageName=$file->getClientOriginalName();
            $news->body=$body;
            $news->save();
            return redirect('/news');
        }


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
        $new=News::all()->find($id);
        return view('news.show',compact('new'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
        $new=News::all()->find($id);
        return view('news.edit',compact('new'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {

        $new=News::all()->find($id);
        $file = Request::file('fileName');
        Storage::disk('local')->put($file->getClientOriginalName(),  File::get($file));
        $title=Request::get('title');
        $abstract=Request::get('abstract');
        $body=Request::get('body');
        $new->title=$title;
        $new->abstract=$abstract;
        $new->imageName=$file->getClientOriginalName();
        $new->body=$body;
        $new->save();
        return redirect('/news');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $new=News::all()->find($id);
        $new->delete();
        return redirect('/news');

    }
}
